home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: strange problem with strcpy()
- Date: 1 Jan 1996 01:39:27 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4c7viv$5vk@umbc9.umbc.edu>
- References: <DKFsII.FyJ@iglou.com> <4c7i49$q7b@news.infi.net>
- NNTP-Posting-Host: f-umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Greg DiGiorgio <nngis@norfolk.infi.net> wrote:
- |> Now, what could cause your problem? As you know STRCPY is a dumb function
- |> that copies bytes from whatever source address you give, up utnil it hits a
- |> NULL to whatever destination address you give. Since you say you are using
- |> MALLOC and/or REALLOC, my guess is that you are screwing up your pointer
- |> arithmetic. It happens to everyone - experienced and newbie alike!
- |>
- |> Let's assume that you have 2 vars, s1 and s2. s1 = "ABCD" and s2 =
- |> (char)NULL. So if you "strcpy(s1,s2)" you'll get "\0BCD" in s1. If your
- |> malloc fails, it returns a NULL which is easy enough to check.
-
- You seriously need to learn the difference between NULL and NUL. They
- are not the same and you should read the FAQ to see what I mean. The
- following code gives a segmentation fault on the UNIX system I am using
- which would tend to place your above assumption in doubt:
-
- #include <stdio.h>
- #include <string.h>
-
- int main (void)
- {
- char *s1 = "ABCD",
- *s2 = NULL;
-
- printf ("%s\n", s1);
- strcpy (s1, s2);
- printf ("%s\n", s1);
-
- return (0);
- }
-
- This demonstrates the critical difference between NULL and NUL. Had I
- wanted to achieve the desired result you claimed would occur I would
- need to change s2 to be = to "" instead of NULL. Like I said please read
- the FAQ before giving anymore incorrect advice.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-